package com.netposa.datawarehouse.bodysearch.web.model;

import java.beans.ConstructorProperties;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class Querypolicy {
   private int step;
   private long range;
   private Long startTime;
   private Long endTime;
   private String lastPassId;
   private String order;
   private List result;
   private Long srcStart;
   private Long srcEnd;
   private int pageLimit;
   private int singleQueryCount;
   private long defaultQueryTimeRange = 3600000L;
   private static long DEFAULT_QUERY_MAXRANGE = 604800000L;

   public Querypolicy addResult(List bodyList) {
      this.singleQueryCount = bodyList.size();
      this.result.addAll(bodyList);
      return this;
   }

   public Querypolicy next() {
      this.calRange().calPolicyTime().calLassPassId().calQueryLimit();
      ++this.step;
      return this;
   }

   public Querypolicy first() {
      long srcRange = this.srcEnd - this.srcStart;
      this.range = srcRange <= this.defaultQueryTimeRange ? srcRange : this.defaultQueryTimeRange;
      if ("ASC".equalsIgnoreCase(this.order)) {
         this.startTime = this.srcStart;
         this.endTime = this.addTime(this.startTime, this.range);
      }

      if ("DESC".equalsIgnoreCase(this.order)) {
         if (StringUtils.isBlank(this.lastPassId)) {
            this.endTime = this.srcEnd;
         } else {
            this.endTime = this.srcEnd;
         }

         this.startTime = this.addTime(this.endTime, -this.range);
      }

      return this;
   }

   private Querypolicy calRange() {
      long msRange = this.defaultQueryTimeRange * (long)Math.pow(4.0, (double)this.step);
      this.range = msRange <= DEFAULT_QUERY_MAXRANGE ? msRange : DEFAULT_QUERY_MAXRANGE;
      return this;
   }

   private Querypolicy calPolicyTime() {
      Long nextStart;
      if ("ASC".equalsIgnoreCase(this.order)) {
         nextStart = this.addTime(this.endTime, this.range);
         this.startTime = this.endTime;
         this.endTime = nextStart < this.srcEnd ? nextStart : this.srcEnd + 1000L;
      }

      if ("DESC".equalsIgnoreCase(this.order)) {
         nextStart = this.addTime(this.startTime, -this.range);
         this.endTime = this.startTime;
         this.startTime = nextStart >= this.srcStart ? nextStart : this.srcStart;
      }

      return this;
   }

   private Querypolicy calLassPassId() {
      if (this.result.size() > 0) {
         this.lastPassId = ((Body)this.result.get(this.result.size() - 1)).getRecordId();
      }

      return this;
   }

   private Querypolicy calQueryLimit() {
      this.pageLimit -= this.singleQueryCount;
      return this;
   }

   public boolean queryOnce() {
      return this.step > 1;
   }

   private Long addTime(Long origTime, long msRange) {
      return origTime + msRange;
   }

   @ConstructorProperties({"step", "range", "startTime", "endTime", "lastPassId", "order", "result", "srcStart", "srcEnd", "pageLimit", "singleQueryCount", "defaultQueryTimeRange"})
   Querypolicy(int step, long range, Long startTime, Long endTime, String lastPassId, String order, List result, Long srcStart, Long srcEnd, int pageLimit, int singleQueryCount, long defaultQueryTimeRange) {
      this.step = step;
      this.range = range;
      this.startTime = startTime;
      this.endTime = endTime;
      this.lastPassId = lastPassId;
      this.order = order;
      this.result = result;
      this.srcStart = srcStart;
      this.srcEnd = srcEnd;
      this.pageLimit = pageLimit;
      this.singleQueryCount = singleQueryCount;
      this.defaultQueryTimeRange = defaultQueryTimeRange;
   }

   public static QuerypolicyBuilder builder() {
      return new QuerypolicyBuilder();
   }

   public int getStep() {
      return this.step;
   }

   public long getRange() {
      return this.range;
   }

   public Long getStartTime() {
      return this.startTime;
   }

   public Long getEndTime() {
      return this.endTime;
   }

   public String getLastPassId() {
      return this.lastPassId;
   }

   public String getOrder() {
      return this.order;
   }

   public List getResult() {
      return this.result;
   }

   public Long getSrcStart() {
      return this.srcStart;
   }

   public Long getSrcEnd() {
      return this.srcEnd;
   }

   public int getPageLimit() {
      return this.pageLimit;
   }

   public int getSingleQueryCount() {
      return this.singleQueryCount;
   }

   public long getDefaultQueryTimeRange() {
      return this.defaultQueryTimeRange;
   }

   public void setStep(int step) {
      this.step = step;
   }

   public void setRange(long range) {
      this.range = range;
   }

   public void setStartTime(Long startTime) {
      this.startTime = startTime;
   }

   public void setEndTime(Long endTime) {
      this.endTime = endTime;
   }

   public void setLastPassId(String lastPassId) {
      this.lastPassId = lastPassId;
   }

   public void setOrder(String order) {
      this.order = order;
   }

   public void setResult(List result) {
      this.result = result;
   }

   public void setSrcStart(Long srcStart) {
      this.srcStart = srcStart;
   }

   public void setSrcEnd(Long srcEnd) {
      this.srcEnd = srcEnd;
   }

   public void setPageLimit(int pageLimit) {
      this.pageLimit = pageLimit;
   }

   public void setSingleQueryCount(int singleQueryCount) {
      this.singleQueryCount = singleQueryCount;
   }

   public void setDefaultQueryTimeRange(long defaultQueryTimeRange) {
      this.defaultQueryTimeRange = defaultQueryTimeRange;
   }

   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof Querypolicy)) {
         return false;
      } else {
         Querypolicy other = (Querypolicy)o;
         if (!other.canEqual(this)) {
            return false;
         } else if (this.getStep() != other.getStep()) {
            return false;
         } else if (this.getRange() != other.getRange()) {
            return false;
         } else {
            label112: {
               Object this$startTime = this.getStartTime();
               Object other$startTime = other.getStartTime();
               if (this$startTime == null) {
                  if (other$startTime == null) {
                     break label112;
                  }
               } else if (this$startTime.equals(other$startTime)) {
                  break label112;
               }

               return false;
            }

            label105: {
               Object this$endTime = this.getEndTime();
               Object other$endTime = other.getEndTime();
               if (this$endTime == null) {
                  if (other$endTime == null) {
                     break label105;
                  }
               } else if (this$endTime.equals(other$endTime)) {
                  break label105;
               }

               return false;
            }

            Object this$lastPassId = this.getLastPassId();
            Object other$lastPassId = other.getLastPassId();
            if (this$lastPassId == null) {
               if (other$lastPassId != null) {
                  return false;
               }
            } else if (!this$lastPassId.equals(other$lastPassId)) {
               return false;
            }

            label91: {
               Object this$order = this.getOrder();
               Object other$order = other.getOrder();
               if (this$order == null) {
                  if (other$order == null) {
                     break label91;
                  }
               } else if (this$order.equals(other$order)) {
                  break label91;
               }

               return false;
            }

            Object this$result = this.getResult();
            Object other$result = other.getResult();
            if (this$result == null) {
               if (other$result != null) {
                  return false;
               }
            } else if (!this$result.equals(other$result)) {
               return false;
            }

            label77: {
               Object this$srcStart = this.getSrcStart();
               Object other$srcStart = other.getSrcStart();
               if (this$srcStart == null) {
                  if (other$srcStart == null) {
                     break label77;
                  }
               } else if (this$srcStart.equals(other$srcStart)) {
                  break label77;
               }

               return false;
            }

            label70: {
               Object this$srcEnd = this.getSrcEnd();
               Object other$srcEnd = other.getSrcEnd();
               if (this$srcEnd == null) {
                  if (other$srcEnd == null) {
                     break label70;
                  }
               } else if (this$srcEnd.equals(other$srcEnd)) {
                  break label70;
               }

               return false;
            }

            if (this.getPageLimit() != other.getPageLimit()) {
               return false;
            } else if (this.getSingleQueryCount() != other.getSingleQueryCount()) {
               return false;
            } else if (this.getDefaultQueryTimeRange() != other.getDefaultQueryTimeRange()) {
               return false;
            } else {
               return true;
            }
         }
      }
   }

   protected boolean canEqual(Object other) {
      return other instanceof Querypolicy;
   }

   public int hashCode() {
      
      int result = 1;
      result = result * 59 + this.getStep();
      long $range = this.getRange();
      result = result * 59 + (int)($range >>> 32 ^ $range);
      Object $startTime = this.getStartTime();
      result = result * 59 + ($startTime == null ? 43 : $startTime.hashCode());
      Object $endTime = this.getEndTime();
      result = result * 59 + ($endTime == null ? 43 : $endTime.hashCode());
      Object $lastPassId = this.getLastPassId();
      result = result * 59 + ($lastPassId == null ? 43 : $lastPassId.hashCode());
      Object $order = this.getOrder();
      result = result * 59 + ($order == null ? 43 : $order.hashCode());
      Object $result = this.getResult();
      result = result * 59 + ($result == null ? 43 : $result.hashCode());
      Object $srcStart = this.getSrcStart();
      result = result * 59 + ($srcStart == null ? 43 : $srcStart.hashCode());
      Object $srcEnd = this.getSrcEnd();
      result = result * 59 + ($srcEnd == null ? 43 : $srcEnd.hashCode());
      result = result * 59 + this.getPageLimit();
      result = result * 59 + this.getSingleQueryCount();
      long $defaultQueryTimeRange = this.getDefaultQueryTimeRange();
      result = result * 59 + (int)($defaultQueryTimeRange >>> 32 ^ $defaultQueryTimeRange);
      return result;
   }

   public String toString() {
      return "Querypolicy(step=" + this.getStep() + ", range=" + this.getRange() + ", startTime=" + this.getStartTime() + ", endTime=" + this.getEndTime() + ", lastPassId=" + this.getLastPassId() + ", order=" + this.getOrder() + ", result=" + this.getResult() + ", srcStart=" + this.getSrcStart() + ", srcEnd=" + this.getSrcEnd() + ", pageLimit=" + this.getPageLimit() + ", singleQueryCount=" + this.getSingleQueryCount() + ", defaultQueryTimeRange=" + this.getDefaultQueryTimeRange() + ")";
   }

   public static class QuerypolicyBuilder {
      private int step;
      private long range;
      private Long startTime;
      private Long endTime;
      private String lastPassId;
      private String order;
      private List result;
      private Long srcStart;
      private Long srcEnd;
      private int pageLimit;
      private int singleQueryCount;
      private long defaultQueryTimeRange;

      QuerypolicyBuilder() {
      }

      public QuerypolicyBuilder step(int step) {
         this.step = step;
         return this;
      }

      public QuerypolicyBuilder range(long range) {
         this.range = range;
         return this;
      }

      public QuerypolicyBuilder startTime(Long startTime) {
         this.startTime = startTime;
         return this;
      }

      public QuerypolicyBuilder endTime(Long endTime) {
         this.endTime = endTime;
         return this;
      }

      public QuerypolicyBuilder lastPassId(String lastPassId) {
         this.lastPassId = lastPassId;
         return this;
      }

      public QuerypolicyBuilder order(String order) {
         this.order = order;
         return this;
      }

      public QuerypolicyBuilder result(List result) {
         this.result = result;
         return this;
      }

      public QuerypolicyBuilder srcStart(Long srcStart) {
         this.srcStart = srcStart;
         return this;
      }

      public QuerypolicyBuilder srcEnd(Long srcEnd) {
         this.srcEnd = srcEnd;
         return this;
      }

      public QuerypolicyBuilder pageLimit(int pageLimit) {
         this.pageLimit = pageLimit;
         return this;
      }

      public QuerypolicyBuilder singleQueryCount(int singleQueryCount) {
         this.singleQueryCount = singleQueryCount;
         return this;
      }

      public QuerypolicyBuilder defaultQueryTimeRange(long defaultQueryTimeRange) {
         this.defaultQueryTimeRange = defaultQueryTimeRange;
         return this;
      }

      public Querypolicy build() {
         return new Querypolicy(this.step, this.range, this.startTime, this.endTime, this.lastPassId, this.order, this.result, this.srcStart, this.srcEnd, this.pageLimit, this.singleQueryCount, this.defaultQueryTimeRange);
      }

      public String toString() {
         return "Querypolicy.QuerypolicyBuilder(step=" + this.step + ", range=" + this.range + ", startTime=" + this.startTime + ", endTime=" + this.endTime + ", lastPassId=" + this.lastPassId + ", order=" + this.order + ", result=" + this.result + ", srcStart=" + this.srcStart + ", srcEnd=" + this.srcEnd + ", pageLimit=" + this.pageLimit + ", singleQueryCount=" + this.singleQueryCount + ", defaultQueryTimeRange=" + this.defaultQueryTimeRange + ")";
      }
   }
}
